<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="12" class="right">
        <el-form class="login-form" :rules="rules" :model="user" status-icon ref="loginForms">
          <el-form-item prop="username" >
            <el-input placeholder="Please username" :prefix-icon="User" v-model="user.username"
              class=".login-input"></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input placeholder="Please password" :prefix-icon="Search" show-password v-model="user.password"
              class=".login-input"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="isLoading" @click="login" class="login-button">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
  //引入icon
  import { User, Search } from '@element-plus/icons-vue'
  //导入ref
  import { ref } from 'vue'
  //引入用户的小仓库
  import { useUserStore } from '@/store/modules/user'
  const userStore = useUserStore()
  //引入el组件的提示框
  import { ElMessage, ElNotification } from 'element-plus'
  //引入路由
  import { useRouter } from 'vue-router'
  //引入获取事件的工具
  import { getTime } from '@/utils/getTime'

  const router = useRouter()
  const isLoading = ref(false)
  //绑定数据
  const user = ref({
    username: 'admin',
    password: '111111',
  })
  const loginForms = ref(null)
  //按钮的点击事件
  const login = async () => {
    await loginForms.value.validate()

    isLoading.value = true
    try {
      await userStore.userLogin(user.value)
      router.push('/')
      ElNotification({
        type: 'success',
        message: '登录成功',
        title: `HI,${getTime()}好`,
        duration: 2000,
      })
      // 登录成功， 加载消失
      isLoading.value = false
    } catch (error) {
      // 登录失败， 加载消失
      isLoading.value = false
      ElNotification({
        type: 'error',
        message: error.message,
        duration: 2000,
      })
    }
  }
  //表单校验的rules
  const rules = {
    //校验属性意思：
    //required： 是否必填
    //min： 最小长度
    //max： 最大长度
    //pattern： 正则校验
    //trigger： 触发校验方式，默认是blur 
    //message： 错误提示信息

    username: [
      {
        required: true,
        min:3,
        max: 12,
        message: '用户名长度在6-12位之间',
        trigger: 'change',
      },
    ],
    password: [
      {
        required: true,
        min:3,
        max: 12,
        message: '用户名长度在6-12位之间',
        trigger: 'change',
      },
    ],
  }
</script>

<style scoped lang="scss">
  .login-container {
    height: 100vh;
    width: 100%;
    background: url('@/assets/images/background.jpg') no-repeat no-repeat;
    background-size: cover;

    .right {
      //border: 1px solid #ccc;
      position: relative;
      top: 30vh;
      margin: 0 auto;
      background: url('@/assets/images/login_form.png') no-repeat no-repeat;
      background-size: cover;
      padding: 20px 30px;

      .login-form {
        //border: 1px solid #ccc;
        width: 100%;
        //居中
        margin: 0 auto;

        .login-button {
          width: 100%;
        }
      }
    }
  }
</style>
